package com.thientan.controller

import java.text.SimpleDateFormat

import org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass


import com.thientan.model.ThongKe
import com.thientan.model.ThongKePhieuThu
import com.thientan.service.ThongKePhieuThuService
import com.thientan.utils.Constants

class ThongKePhieuThuController extends AbstractBaseController {
	ThongKePhieuThuService thongKePhieuThuService

	@Override
	public Class getDomainClass() {
		return ThongKePhieuThu.class
	}

	@Override
	public String getDomainName() {
		message(code : "default.thientan.messages.thongKe")
	}

	@Override
	public Object getDomainService() {
		return thongKePhieuThuService
	}

	@Override
	public String getDisplayFieldName() {
		null
	}

	@Override
	protected String getExportFileName() {
		"ThongKePhieuThu"
	}

	protected Map buildExportData(Map exportParams){
		def sdfToDate = new SimpleDateFormat(Constants.TO_DATE_FORMAT)
		def sdfDateTo = new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT)
		Date fromDate = params["fromDate"] != "" ? sdfToDate.parse(params["fromDate"]) : null
		Date toDate = params["toDate"] != "" ? sdfToDate.parse(params["toDate"]) : null
		List data = ThongKePhieuThu.withCriteria {
			if (fromDate) {
				ge("ngayLap", fromDate)
			}
			if (toDate) {
				le("ngayLap", toDate)
			}
		}
		return [
			data : extractPropertiesAsMaps(data, new DefaultGrailsDomainClass(ThongKePhieuThu.class).properties*.name),
			"ngayXuat" : sdfDateTo.format(new Date()),
			"thoiGian" : (fromDate ? sdfDateTo.format(fromDate) : "") + " - " + (toDate ? sdfDateTo.format(toDate) : "")
		]
	}
}
